package org.db2.project.airline.dao.impl;

import org.db2.project.airline.dao.BookingSeatDAO;
import org.db2.project.airline.dao.DAOException;
import org.db2.project.airline.model.BookingSeat;
import org.db2.project.airline.model.FlightClass;
import org.hibernate.HibernateException;

/**
 * Created by IntelliJ IDEA. User: jakub Date: 22.04.11 Time: 11:46 To change this template use File | Settings | File
 * Templates.
 */
public class HibernateBookingSeatDAO extends HibernateGenericDAO<BookingSeat> implements BookingSeatDAO {

    public HibernateBookingSeatDAO() {
        // empty constructor
    }

    @Override
    public long getMaxSeatId(FlightClass flightClass) throws DAOException {

        try {
            return (Long)getSession().createQuery(
                    "select coalesce(max(bs.seat), 0) from BookingSeat bs join bs.flightClass fc where fc.id = :flightClassId")
                    .setParameter("flightClassId", flightClass.getId())
                    .uniqueResult();
        } catch(HibernateException exc) {

            throw new DAOException("Error occurred when retrieving entity from persistence.");
        }
    }
}
